package biscuit;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biscuit.data.Document;
import biscuit.data.Room;

import com.google.gson.Gson;

@WebServlet(urlPatterns = "/timemachine")
public class TimemachineServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3137499519028999350L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int index = Integer.parseInt((String) request.getParameter("index"));
		
		// 방 리스트를 추출해서 방 이름 리스트를 만든다.
		@SuppressWarnings("unchecked")
		List<Document> docuHistory = ((List<Room>) getServletContext().getAttribute("roomList")).get(index).getDocuHistory();

		// JSON 으로 결과를 반환한다.
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		response.getWriter().print(new Gson().toJson(docuHistory));
	}
}
